package ru.edu.vstu.atpp.acm.ui.pages;


import org.apache.tapestry5.annotations.InjectComponent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Tree;
import org.apache.tapestry5.services.PersistentLocale;
import org.apache.tapestry5.tree.DefaultTreeModel;
import org.apache.tapestry5.tree.TreeModel;
import org.hibernate.Session;
import ru.edu.vstu.atpp.acm.ui.entities.Tema;
import ru.edu.vstu.atpp.acm.ui.services.dao.TemaDAO;
import ru.edu.vstu.atpp.acm.ui.services.encoder.TemaEncoder;
import ru.edu.vstu.atpp.acm.utils.TemaTreeModelAdapter;


import javax.inject.Inject;
import java.util.Locale;

public class TaskListPage {

    @Inject
    private TemaDAO temaDAO;

    @Inject
    private Locale currentLocale;

    @InjectComponent
    private Tree tree;

    private TreeModel<Tema> temaTreeModel;

    private Tema tema;

    @Property
    private Tema currentTema;

    public void onActivate(Tema tema) {
        this.tema = tema;
    }


    public TreeModel<Tema> getTemaTreeModel() {
        if (temaTreeModel == null) {
            temaTreeModel = new DefaultTreeModel<Tema>(new TemaEncoder(temaDAO)
                    , new TemaTreeModelAdapter(temaDAO, currentLocale), temaDAO.getRootValues());
        }
        return temaTreeModel;
    }




    public String getTemaName() {
        return currentTema.getMultilingualId().getLocalizedString(currentLocale);
    }
}